//
//  WechatSportController.swift
//  Alamofire
//
//  Created by 易先生 on 2022/8/10.
//

import UIKit
import MYModule
import RxCocoa
import RxSwift
//import Toast_Swift

class WechatSportController: ViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
//        self.view.makeToastActivity(.center)
        self.bindWechatFit()
    }

    override func makeUI() {
        super.makeUI()

        view.backgroundColor =  UIColor.darkBackground
        view.addSubview(titleView)
        view.addSubview(scrollView)
        view.addSubview(ununitedView)
        scrollView.addSubview(contentView)
        contentView.addSubview(qrCodeView)
        contentView.addSubview(wechatView)
        contentView.addSubview(scanCodeView)
        contentView.addSubview(connectionView)
        contentView.addSubview(qrCodeBgView)
        qrCodeBgView.addSubview(qrCodeImageView)
        contentView.addSubview(saveButton)


        titleView.snp.makeConstraints { make in
            make.top.equalTo(topLayoutGuide.snp.bottom)
            make.leading.trailing.equalToSuperview()
        }

        scrollView.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom)
            make.leading.trailing.bottom.equalToSuperview()
        }

        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.centerX.equalToSuperview()
        }

        qrCodeView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(44)
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().offset(-20)
            make.height.equalTo(70)
        }

        wechatView.snp.makeConstraints { make in
            make.top.equalTo(qrCodeView.snp.bottom)
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().offset(-20)
            make.height.equalTo(70)
        }

        scanCodeView.snp.makeConstraints { make in
            make.top.equalTo(wechatView.snp.bottom)
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().offset(-20)
            make.height.equalTo(70)
        }

        connectionView.snp.makeConstraints { make in
            make.top.equalTo(scanCodeView.snp.bottom)
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().offset(-20)
            make.height.equalTo(70)
        }

        qrCodeBgView.snp.makeConstraints { make in
            make.top.equalTo(connectionView.snp.bottom).offset(50)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 130, height: 130))
        }

        qrCodeImageView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 110, height: 110))
        }

        saveButton.snp.makeConstraints { make in
            make.top.equalTo(qrCodeBgView.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 90, height: 24))
            make.bottom.equalToSuperview().offset(-150)
        }

        ununitedView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }

    }

    private func bindWechatFit() {
        titleView.configTitle(title: "wechat_sport_title".language)
//        let pid = Bands.sharedInstance.getPid()
//        let mac = User.sharedInstance.bindMac()
//        guard mac.count > 0, pid.count > 0 else {
//            self.view.hideToastActivity()
//            return
//        }
        titleView.configTitle(title: "wechat_sport_access".language)
        scrollView.isHidden = false
        self.bindDevice()

    }

    private func bindDevice() {
//        let pid = Bands.sharedInstance.getPid()
//        let mac = User.sharedInstance.bindMac()
//        let mToken = LoginModel.getToken()

//        let parameters: Parameters = ["pid": pid, "mac": mac, "token": mToken]
//
//
//
//        Http.requestJSON(apiGetQrcode, method: .get, parameters: parameters) { [weak self] response in
//            guard let self = self else { return }
//            self.view.hideToastActivity()
//            switch response.result {
//            case .success(let json):
//                let data = json as AnyObject
//                let code = data.value(forKey: "code") as? Int
//                if (code == 0) {
//                    if let qrticket = data.value(forKey: "qrticket") as? String {
//                        let qrCode = QRCode.init(qrticket)
//                        self.qrCodeImageView.image = qrCode?.image
//                        self.qrCodeBgView.isHidden = false
//                        self.saveButton.isHidden = false
//                    }
//                }
//            case .failure(_):
//                self.view.makeToast("网络请求失败", duration: 10.0, position: .center)
//            }
//        }
    }

    @objc private func saveQrcodeImage() {
        let qrImage = self.qrCodeImageView.screenShot()
        UIImageWriteToSavedPhotosAlbum(qrImage!, self, #selector(self.savedImage(image: didFinishSavingWithError: contextInfo:)), nil)
        let urlStr = "weixin://"
        if let url = URL(string: urlStr) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options: Dictionary(), completionHandler: nil)
            } else {
                // Fallback on earlier versions
                UIApplication.shared.openURL(url)
            }
        }
    }

    @objc func savedImage(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: AnyObject) {

//        if error != nil {
//            self.view.makeToast("wechat_sport_qr_code_save_success".language, duration: 2.0, position: .center)
//        } else {
//            self.view.makeToast("wechat_sport_qr_code_save_fail".language, duration: 2.0, position: .center)
//        }
    }


    private lazy var ununitedView: WechatSportsUnunitedView = {
        let ununitedView = WechatSportsUnunitedView()
        ununitedView.isHidden = true
        return ununitedView
    }()


    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.isHidden = true
        return scrollView
    }()

    private lazy var contentView: View = {
        let contentView = View()
        return contentView
    }()

    private lazy var titleView: TopTitleView = {
        let titleView = TopTitleView()
        return titleView
    }()


    private lazy var qrCodeView: WechatSportGuidView = {
        let qrCodeView = WechatSportGuidView()
        qrCodeView.configImageOrContent(imageName: "wechat_sport_code", title: "wechat_sport_qr_code_step_1".language)
        return qrCodeView
    }()

    private lazy var wechatView: WechatSportGuidView = {
        let wechatView = WechatSportGuidView()
        wechatView.configImageOrContent(imageName: "wechat_sports_wechat", title: "wechat_sport_qr_code_step_2".language)
        return wechatView
    }()

    private lazy var scanCodeView: WechatSportGuidView = {
        let scanCodeView = WechatSportGuidView()
        scanCodeView.configImageOrContent(imageName: "wechat_sports_scanCode", title: "wechat_sport_qr_code_step_3".language)
        return scanCodeView
    }()

    private lazy var connectionView: WechatSportGuidView = {
        let connectionView = WechatSportGuidView()
        connectionView.configImageOrContent(imageName: "wechat_sports_connection", title: "wechat_sport_qr_code_step_4".language)
        connectionView.nextImageView.isHidden = true
        return connectionView
    }()

    private lazy var qrCodeBgView: View = {
        let qrCodeBgView = View()
//        qrCodeBgView.isHidden = true
        qrCodeBgView.layer.cornerRadius = 10
        qrCodeBgView.layer.borderWidth = 1.0
        qrCodeBgView.layer.borderColor = UIColor.assist_222222.cgColor
        return qrCodeBgView
    }()

    private lazy var qrCodeImageView: ImageView = {
        let qrCodeImageView = ImageView()
        qrCodeImageView.image = UIImage(named: "ic_qrCode")
        return qrCodeImageView
    }()

    private lazy var saveButton: Button = {
        let saveButton = Button(frame: .zero)
//        saveButton.isHidden = true
        saveButton.layer.cornerRadius = 4
        saveButton.backgroundColor = UIColor.theme
        saveButton.setTitle("wechat_sport_qr_code_save".language, for: .normal)
        saveButton.setTitleColor(UIColor.assist_FFFFFF, for: .normal)
        saveButton.titleLabel?.font = Fonts.systemFont(ofSize: 12)
        saveButton.addTarget(self, action: #selector(saveQrcodeImage), for: .touchUpInside)
        return saveButton
    }()
}
